package com.proj.hot.login;

import android.text.TextUtils;

public class MD5Util {

    public static String encode2hex(String source) {
	byte[] data = null;
	if (!TextUtils.isEmpty(source)) {
	    data = encode2bytes(source);
	} else {
	    return null;
	}
	StringBuffer hexString = new StringBuffer();
	for (int i = 0; i < data.length; i++) {
	    String hex = Integer.toHexString(0xff & data[i]);

	    if (hex.length() == 1) {
		hexString.append('0');
	    }
	    hexString.append(hex);
	}
	return hexString.toString();
    }

    private static byte[] encode2bytes(String str) {
	byte data[] = str.getBytes();
	return data;
    }

    /**
     * 验证字符串是否匹配
     * 
     * @param unknown
     *            待验证的字符串
     * @param okHex
     *            使用MD5加密过的16进制字符串
     * @return 匹配返回true，不匹配返回false
     */
    public static boolean validate(String unknown, String okHex) {
	return okHex.equals(encode2hex(unknown));
    }

}
